package one.bugu.android.demon.ui.activity;

import android.annotation.SuppressLint;
import android.text.SpannableString;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.juefeng.android.framework.LKUtil;
import com.juefeng.android.framework.view.annotation.LKContentView;
import com.juefeng.android.framework.view.annotation.LKInjectView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import one.bugu.android.demon.R;
import one.bugu.android.demon.common.BaseHttpAsycResponceHandler;
import one.bugu.android.demon.common.MyBaseActivity;
import one.bugu.android.demon.constant.Constant;
import one.bugu.android.demon.constant.HttpConstant;
import one.bugu.android.demon.ui.dialog.DialogHint;
import one.bugu.android.demon.ui.widget.BaseTopBarView;
import one.bugu.android.demon.util.IntentUtils;
import one.bugu.android.demon.util.MyTextUtils;
import one.bugu.android.demon.util.OnViewClickListener;
import one.bugu.android.demon.util.ParamUtils;
import one.bugu.android.demon.util.PreferencesUtil;
import one.bugu.android.demon.util.StatusBarUtils;

/**
 * PackageName: one.bugu.android.demon.ui.activity.wallet
 * Content: 设置页面页面
 *
 * @author Administrator
 * @date 2018/12/13 9:48
 */
@LKContentView(R.layout.activity_setting)
public class SettingActivity extends MyBaseActivity {
    /**
     * topbar
     */
    @LKInjectView(R.id.btbv_setting)
    private BaseTopBarView btbv_setting;
    /**
     * 头像
     */
    @LKInjectView(R.id.ll_setting_avatar)
    private LinearLayout ll_setting_avatar;

    @LKInjectView(R.id.iv_setting_avatar)
    private ImageView iv_setting_avatar;
    /**
     * 手机号
     */
    @LKInjectView(R.id.ll_setting_phone)
    private LinearLayout ll_setting_phone;

    @LKInjectView(R.id.tv_setting_phone)
    private TextView tv_setting_phone;
    /**
     * 昵称
     */
    @LKInjectView(R.id.ll_setting_name)
    private LinearLayout ll_setting_name;

    @LKInjectView(R.id.tv_setting_name)
    private TextView tv_setting_name;
    /**
     * 客服
     */
    @LKInjectView(R.id.ll_setting_service)
    private LinearLayout ll_setting_service;
    /**
     * 公众号
     */
    @LKInjectView(R.id.ll_setting_vipcn)
    private LinearLayout ll_setting_vipcn;
    /**
     * qq群
     */
    @LKInjectView(R.id.ll_setting_qq)
    private LinearLayout ll_setting_qq;

    @LKInjectView(R.id.tv_setting_qq)
    private TextView tv_setting_qq;
    /**
     * 意见反馈
     */
    @LKInjectView(R.id.ll_setting_feedback)
    private LinearLayout ll_setting_feedback;
    /**
     * 关于我们
     */
    @LKInjectView(R.id.ll_setting_about_us)
    private LinearLayout ll_setting_about_us;
    /**
     * 退出登录
     */
    @LKInjectView(R.id.tv_setting_exit)
    private TextView tv_setting_exit;
    /**
     * 弹框
     */
    private DialogHint dialogHint;

    @Override
    protected void initView() {
        super.initView();
        //设置状态栏
        StatusBarUtils.getInstance().setStatusBar("#FFFFFF", this, btbv_setting, false);
        //退出登录弹框文字设置
        dialogHint = new DialogHint(this, "是否退出登录", true);
    }

    @Override
    protected void initData() {
        super.initData();
        //手机号
        String userTel = PreferencesUtil.getInstance().getString(this, Constant.USERTEL, "");
        tv_setting_phone.setText(userTel);
        //昵称
        String name = PreferencesUtil.getInstance().getString(this, Constant.USER_NAMR, "");
        tv_setting_name.setText(name);
        //头像
        String avatar = PreferencesUtil.getInstance().getString(this, Constant.USER_AVATAR, "");
        LKUtil.getImageLoader().loadRoundImage(avatar, iv_setting_avatar, R.mipmap.nongchang_touxiang);
        //QQ群
        String qqZone = "加QQ群 86661789";
        SpannableString qq = MyTextUtils.getInstance().setAutoTextColor(qqZone, "[0-9]{1,}", "#5856D0");
        tv_setting_qq.setText(qq);
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        btbv_setting.setEventInterface(new BaseTopBarView.EventInterface() {
            @Override
            public void leftOnClick() {
                finish();
            }

            @Override
            public void leftCloseClick() {

            }

            @Override
            public void rightOnClick() {

            }
        });
        //头像
        ll_setting_avatar.setOnClickListener(new OnViewClickListener() {
            @Override
            protected void singleClick(View view) {

            }
        });
        //昵称
        ll_setting_name.setOnClickListener(new OnViewClickListener() {
            @Override
            protected void singleClick(View view) {

            }
        });
        //客服
        ll_setting_service.setOnClickListener(new OnViewClickListener() {
            @Override
            protected void singleClick(View view) {
                List<ParamUtils.NameValue> parList1 = ParamUtils.build().put(WebActivity.TITLE, "添加客服微信").put(WebActivity.URL, HttpConstant.ADD_WEIXIN_URL).create();
                IntentUtils.startAty(SettingActivity.this, WebActivity.class, parList1);
            }
        });
        //公众号
        ll_setting_vipcn.setOnClickListener(new OnViewClickListener() {
            @Override
            protected void singleClick(View view) {
                List<ParamUtils.NameValue> parList = ParamUtils.build().put(WebActivity.TITLE, "关注公众号").put(WebActivity.URL, HttpConstant.WEIXIN_URL).create();
                IntentUtils.startAty(SettingActivity.this, WebActivity.class, parList);
            }
        });
        //意见反馈
        ll_setting_feedback.setOnClickListener(new OnViewClickListener() {
            @Override
            protected void singleClick(View view) {
                IntentUtils.startAty(SettingActivity.this, FeedbackActivity.class);
            }
        });
        //关于我们
        ll_setting_about_us.setOnClickListener(new OnViewClickListener() {
            @Override
            protected void singleClick(View view) {
                IntentUtils.startAty(SettingActivity.this, AboutUsActivity.class);
            }
        });
        //退出按钮
        tv_setting_exit.setOnClickListener(new OnViewClickListener() {
            @Override
            protected void singleClick(View view) {
                dialogHint.show();
            }
        });

        //退出登录弹框
        dialogHint.setOnDialogVersionListener(new DialogHint.OnDialogVersionListener() {
            @Override
            public void onSure() {
                //退出登录后将所有分享的商品信息删除
                PreferencesUtil.getInstance().putString(SettingActivity.this, Constant.SHARE_GOODS, "");
                //调用退出登录方法
                escLogin();
            }

            @Override
            public void onCancel() {

            }
        });

    }

    /**
     * 退出登录
     */
    public void escLogin() {
        String token = PreferencesUtil.getInstance().getString(this, Constant.TOKEN);
        Map<String, String> map = new HashMap<>();
        map.put("token", token);
        LKUtil.getHttpManager().postBody(HttpConstant.ESCLOGIN_URL, map, escHandler);
    }

    /**
     * 退出登录的网络请求
     */
    @SuppressLint("HandlerLeak")
    public BaseHttpAsycResponceHandler<String> escHandler = new BaseHttpAsycResponceHandler<String>(true) {

        @Override
        public void onSuccess(String data) {
            super.onSuccess(data);
            Toast.makeText(SettingActivity.this, "退出成功", Toast.LENGTH_SHORT).show();
            //登录状态改为false 跳转登录页面
            PreferencesUtil.getInstance().putBoolean(SettingActivity.this, Constant.IS_LOGIN, false);
            IntentUtils.startAty(SettingActivity.this, MainActivity.class);
            finish();
        }

        @Override
        public void onFailed(int code, String message) {
            super.onFailed(code, message);

        }

        @Override
        public void onError() {
            super.onError();
        }
    };
}
